from flask import Flask, request
import json
import db

app = Flask(__name__)


# text
@app.route('/hi')
def hi():
    return 'hi~这是一个小彩蛋，愿你开心'


# api接口前缀
# apiPrefix = '/api/v1/'

##################  Staff接口  ##################

# 查看环境管理页面的数据
@app.route('/api/param/list', methods=['POST', 'GET'])
def updateStaff():
    sql = 'select distinct  env_id from environment_param order by env_id desc'
    data = db.query_data(sql)
    # print(data)
    list = []
    for i in range(len(data)):
        dic = {}
        dic['key'] = i
        dic['env_id'] = '环境号' + str(data[i]['env_id'])
        for j in range(3):
            dic['p' + str(j)] = '查看'
        list.append(dic)
    re = {
        'code': 200,
        'data': list,
        'message': "请求成功"
    }
    # print("后端数据：",data)
    return json.dumps(re, ensure_ascii=False)


# 删除环境管理的数据
@app.route('/api/Ambient/Delete', methods=['POST', 'GET'])
def AmbientDelete():
    # temp=json.loads(request.data)[0]['env_id'][3:7]
    env_id = json.loads(request.data)
    for i in range(len(env_id)):
        temp = json.loads(request.data)[i]['env_id'][3:7]
        print(temp)
        sql = "DELETE FROM environment_param WHERE env_id=%s" % temp
        db.delete_or_data(sql)
        # print(json.loads(request.data))
    # print('我是不是要取出的值',env_id)
    # print(json.loads(request.data))
    return json.dumps({'message': '200'})


@app.route('/api/input/inputHome', methods=['GET', 'POST'])
def WhereInputHome():
    # 后置任务
    sql = "SELECT * FROM testcase_poststep"
    data = db.query_data(sql)
    print('后置任务', data)
    # 前置任务
    sql2 = "SELECT * FROM testcase_prestep"
    data2 = db.query_data(sql2)
    print('前置任务', data2)
    # 用例验证
    sql3 = "SELECT * FROM testcase_verify"
    data3 = db.query_data(sql3)
    print('用例验证', data3)
    # 用例故障
    sql4 = "SELECT * FROM testcase_fault"
    data4 = db.query_data(sql4)
    print('用例故障', data4)
    #
    # for i in range(len(data4)):
    #     if data4[i]['tc_id']==data4[i+1]['tc_id']:
    #         print(data4[i]+data4[i+1])

    re = {
        'code': 200,
        'data': data4,
        'message': '请求成功'
    }
    return json.dumps(re)


if __name__ == "__main__":
    app.run(debug=True, port=5001)